package com.zxl.live.screen;

import android.content.Context;
import android.text.TextUtils;

import com.google.android.gms.ads.MobileAds;
import com.liulishuo.filedownloader.FileDownloader;
import com.zxl.live.screen.call.helper.ThemeHelper;
import com.zxl.live.screen.screen.constant.LockerEnv;
import com.zxl.live.screen.screen.constant.ScreenSetting;
import com.zxl.live.screen.screen.ui.receiver.CoreReceiver;
import com.zxl.live.screen.screen.ui.receiver.ScreenReceiver;
import com.zxl.live.screen.screen.ui.service.CoreService;
import com.zxl.live.screen.screen.ui.service.ScreenService;
import com.zxl.live.screen.screen.utils.ScreenUtils;
import com.zxl.live.tools.base.ActivityLifecycle;
import com.zxl.live.tools.base.BaseApp;
import com.zxl.live.tools.common.ApplicationHelper;
import com.zxl.live.tools.configs.AdsConfigsHelper;
import com.zxl.live.tools.tools.MachineUtils;
import com.zxl.live.tools.userstart.UserStartManager;
import com.zxl.process.sdk.DaemonClient;
import com.zxl.process.sdk.DaemonConfigurations;


/**
 * Created by Administrator on 2017/11/9.
 */

public class LiveApp extends BaseApp implements Runnable {

    @Override
    public void onCreate() {
        super.onCreate();
        MobileAds.initialize(this, "ca-app-pub-1417586053260517~7148731433");
        ScreenUtils.reset(this);
        ScreenSetting.init();
        new Thread(this).start();
        String currProcessName = MachineUtils.getCurrProcessName(this);
        if (!TextUtils.isEmpty(currProcessName)) {
            if (currProcessName.equals(LockerEnv.Process.MAIN_PROCESS)) {
                UserStartManager.refreshState(this);
                FileDownloader.setupOnApplicationOnCreate(this);
            } else  if (currProcessName.equals(LockerEnv.Process.CORE_PROCESS)) {
                UserStartManager.refreshState(this);
            } else  if (currProcessName.equals(LockerEnv.Process.SCREEN_PROCESS)) {
                UserStartManager.refreshState(this);
            }
        }
        ScreenService.startCoreService(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //初始化DaemonClient
        DaemonClient.getInstance().init(createDaemonConfigurations());
        DaemonClient.getInstance().onAttachBaseContext(base);
        ApplicationHelper.setUp(this, sWorker, sHandler);
    }

    /**
     * 构建守护配置
     * @return
     */
    private DaemonConfigurations createDaemonConfigurations() {
        DaemonConfigurations configs = new DaemonConfigurations(new DaemonConfigurations.DaemonConfiguration(LockerEnv.Process.SCREEN_PROCESS,
                ScreenService.class.getCanonicalName(), ScreenReceiver.class.getCanonicalName()), new DaemonConfigurations.DaemonConfiguration(
                LockerEnv.Process.CORE_PROCESS, CoreService.class.getCanonicalName(), CoreReceiver.class.getCanonicalName()));
        //开启守护效果统计
        configs.setStatisticsDaemonEffect(true);
        //设置唤醒常驻服务轮询时长
        configs.setDaemonWatchInterval(20);
        //设置唤醒常驻服务轮询时长(android版本21以上)
        configs.setDaemonWatchIntervalAboveAPI21(20);
        //设置多长时间后，第一次唤醒其它常驻进程
        configs.setDaemonOtherPersistentProcessTrigger(2);
        //设置唤醒其它常驻进程的监控时间间隔
//        configs.setDaemonOtherPersistentProcessInterval(60);
        return configs;
    }

    @Override
    public void run() {
        ThemeHelper.init();
        AdsConfigsHelper.refreshControlInfo(this);
        ActivityLifecycle.registerActivityLifecycleCallbacks(this);
    }
}
